package com.fruit.api.controller.fruit;

import com.fruit.bean.dto.fruit.FruitShelfInfoDto;
import com.fruit.bean.entity.fruit.FruitSellRequestInfo;
import com.fruit.bean.entity.fruit.FruitShelfInfo;
import com.fruit.bean.entity.fruit.FruitStockInfo;
import com.fruit.bean.vo.front.Rets;
import com.fruit.service.fruit.FruitSellRequestService;
import com.fruit.service.fruit.FruitShelfService;
import com.fruit.service.fruit.FruitStockService;
import com.fruit.utils.BigDecimalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/fruit/shelf")
public class FruitShelfController {

    @Autowired
    private FruitShelfService shelfService;

    @Autowired
    private FruitSellRequestService sellRequestService;

    /**
     * 上架或下架
     * 0上架-1下架
     * @return
     */
    @PostMapping
    public Object shelfOnOrOff(@RequestParam Long id,
                               @RequestParam Integer fruitShelfOff,
                               @RequestParam(required = false) double sellPrice){
        FruitShelfInfo shelfInfo = shelfService.get(id);
        shelfInfo.setFruitShelfOff(fruitShelfOff);
        if(fruitShelfOff == 0){
            //如果是上架，需要保存上架价格。
            shelfInfo.setFruitSellPrice(sellPrice);
            // 保存代卖最终价格
            FruitSellRequestInfo fruitSellRequestInfo = sellRequestService.get(shelfInfo.getFruitSellRequestId());
            fruitSellRequestInfo.setFruitLastPrice(sellPrice);
            sellRequestService.update(fruitSellRequestInfo);
        }
        shelfService.update(shelfInfo);
        return Rets.success();
    }

    /**
     * 小程序端获取商品列表，返回商品信息
     * @return
     */
    @PostMapping("/list")
    public Object listShelf(){
        List<FruitShelfInfoDto> shelfList = shelfService.userQueryShelfList();
        return Rets.success(shelfList);
    }

    /**
     * 获取商品详情
     * @param data 商品id，shelfId
     * @return
     */
    @PostMapping("/details")
    public Object shelfDetails(@RequestBody Map<String,Long> data){
        FruitShelfInfoDto shelfInfoDto = shelfService.getShelfDetails(data.get("id"));
        return Rets.success(shelfInfoDto);
    }

    /**
     * 管理端获取库存信息列表
     * @return
     */
    @GetMapping("/list")
    public Object listStockInfo(){
        return Rets.success(shelfService.queryShelfPage());
    }
}
